﻿using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;

namespace Controllers
{
    public class DeptController : BaseController
    {
        private string jsonStr = "";
        private IList<Depts> depts;

        private IList<int> deptIdList=new List<int>();//当前部门和所有子部门Id列表

        //根据Controller获得所有权限部门Id列表,放入DeptIdList
        public IList<int> GetDeptIdListByController(string controllerName, List<int> empRoleIds, List<int> sessionDeptIdList)
        {
            //按角色获取菜单
            IQueryable<int?> deptIdArr = (from r in ctx.RoleControllerDepts
                                            where empRoleIds.Contains(r.RoleId) && (r.Controller == controllerName || r.Controller==null)
                                            select r.DeptId).Distinct();
            if (deptIdArr.Contains(null))
            {
                GetChildDeptIdList(1);//所有部门
            }
            else
            {
                deptIdList = sessionDeptIdList;
                foreach (int dId in deptIdArr)
                {
                    if (!deptIdList.Contains(dId))
                    {
                        deptIdList.Add(dId);
                    }
                }
            }
            return deptIdList;
        }

        //根据部门Id获得当前部门和所有子部门Id列表,放入DeptIdList
        public IList<int> GetChildDeptIdList(int deptId)
        {
            getChildDeptIdList(deptId);
            return deptIdList;
        }
        private void getChildDeptIdList(int deptId)
        {
            var deptAll = CommonController.DeptAll;
            var ds = deptAll.Where(d => d.Pid == deptId);//选择下级子节点
            if (ds.Count() > 0)//非叶子节点
            {
                deptIdList.Add(deptId);
                foreach (var d in ds)
                {
                    getChildDeptIdList(d.Id);
                }
            }
            else//叶子菜单
            {
                deptIdList.Add(deptId);
            }
        }

        //部门树(不含职员,不含checkbox),按部门Id生成
        public ActionResult JsonTree(int deptId)
        {
            depts =CommonController.DeptAll;
            var rootDepts = depts.Where(d => d.Pid == deptId).Select(d => d);//子节点
            if (deptId == 0)
            {
                rootDepts = depts.Where(d => d.Pid.Equals(null)).Select(d => d);//子节点
            }
            jsonStr = "";
            foreach (var d in rootDepts)
            {
                constructJsonFromHeadNode(d);
            }
            jsonStr = "[" + jsonStr + "]";
            jsonStr = jsonStr.Replace(",]", "]");
            return Content(jsonStr);
        }
        //递归调用,循环生成json数据
        private void constructJsonFromHeadNode(Depts dept)
        {
            var ds = depts.Where(d => d.Pid == dept.Id).Select(d => d);//选择下级子节点
            if (ds.Count() > 0)//非叶子节点
            {
                jsonStr += "{id:'" + dept.Id + "',text:'" + dept.Text + "',cls:'dept-" + dept.Id + "',expanded:false,children:[";
                foreach (var d in ds)
                {
                    constructJsonFromHeadNode(d);
                }
                jsonStr += "]},";
            }
            else//叶子菜单
            {
                jsonStr += "{id:'" + dept.Id + "',text:'" + dept.Text + "',cls:'dept-" + dept.Id + "',leaf:true},";
            }
        }

        //部门树,按登录人员Id生成
        public ActionResult DeptTreeByEmp(string controllerName)
        {
            IQueryable<int?> deptIdArr = null;
            EmpBasicInfo ebi = Session["ebi"] as EmpBasicInfo;
            if (controllerName != null)
            {
                IList<EmpRole> empRole = ebi.EmpRoles;
                List<int> empRoleIds = new List<int>();
                foreach (EmpRole er in empRole)
                {
                    empRoleIds.Add(er.RoleId);
                }
                //按角色获取菜单
                deptIdArr = (from r in ctx.RoleControllerDepts where empRoleIds.Contains(r.RoleId) && (r.Controller == controllerName||r.Controller==null) select r.DeptId).Distinct();
            }
            depts = CommonController.DeptAll;
            IList<int> deptTopIds = Session["deptTopIds"] as IList<int>;
            if (deptIdArr != null && deptIdArr.Contains(null) || ebi.EmpRoles.Where(r => r.RoleId == 1).Any())
            {
                var r = depts.Where(d => d.Pid ==null).Single();//整个公司
                constructJsonFromHeadNode(r);
                jsonStr = "[" + jsonStr + "]";
                jsonStr = jsonStr.Replace(",]", "]");
            }
            else if (deptTopIds.Count() > 0 || deptIdArr != null && deptIdArr.Count() > 0)
            {
                foreach (var d0 in deptTopIds)
                {
                    var r = depts.Where(d => d.Id == d0).Single();//子节点
                    constructJsonFromHeadNode(r);
                }
                if (deptIdArr != null && deptIdArr.Count() > 0)
                {
                    foreach (var d0 in deptIdArr)
                    {
                        if(jsonStr.IndexOf("'"+d0+"'")>0)
                        {
                            continue;
                        }
                        var r = depts.Where(d => d.Id == d0).Single();//子节点
                        constructJsonFromHeadNode(r);
                    }
                }
                jsonStr = "[" + jsonStr + "]";
                jsonStr = jsonStr.Replace(",]", "]");
            }
            else
            {
                jsonStr = "[]";
            }
            return Content(jsonStr);
        }

        //获取申请人部门名(多部门取职位最高者)
        public string FindNameById(int? id)
        {
            if(id==null)
            {
                EmpBasicInfo ebi = Session["ebi"] as EmpBasicInfo;
                EmpDepPosition edp=ebi.EmpDepPos.Where(r => r.PosId <= 7).OrderByDescending(r => r.PosId).First();
                id = edp.DeptId;
            }
            var e = ctx.Depts.Single(r => r.Id == id);
            return e.Text;
        }
    }
}